package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.SistemaCVR;

/**
 * Servlet implementation class RegistrarRecepcionCliente
 */
public class RegistrarRecepcionCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public RegistrarRecepcionCliente() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			request.setAttribute("tipo", "recepcion en cliente");
			
			String cuit = request.getParameter("cuitCliente");
			int nroRemito = Integer.parseInt(request.getParameter("nroRemito"));
			String sFechaRecepcion = (String)request.getParameter("fechaRecepcion").trim();
		
			SimpleDateFormat formato = new SimpleDateFormat("ddMMyyyy");
			Date fechaRecepcion = null;
			formato.setLenient(false);
		
			fechaRecepcion = (Date)formato.parse(sFechaRecepcion.trim());
			SistemaCVR.registrarRecepcionEnCliente(cuit, nroRemito, fechaRecepcion);
			
			request.setAttribute("error", "");
			
		}catch (ParseException pe) {
			request.setAttribute("error", "Revise el formato de la fecha ingresada.");
		}catch (Exception e) {
			request.setAttribute("error", e.getMessage());
		} finally {
			RequestDispatcher dispatcher = request.getRequestDispatcher("/resultadoRegistrar.jsp");
			dispatcher.forward(request, response);
		}
	
	}
}
